package com.yg.controller;


import com.yg.config.FilePathConfig;
import com.yg.config.result.R;
import com.yg.entity.singleEntity.StartupProject;
import com.yg.service.StartupProjectService;
import com.yg.utils.CloseStream;
import com.yg.utils.DownLoadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * <p>
 * 对应sheet12，此表是大学生创新创业项目表 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/startupProject")
@CrossOrigin
public class StartupProjectController {
    @Autowired
    private StartupProjectService startupProjectService;

    @Autowired
    private FilePathConfig filePathConfig;

    /**
     * 读取excel写入数据库
     * clr
     *
     * @param file
     * @return
     */
    @PostMapping("/readExcel")
    public R readExcel(MultipartFile file) {
        //获取fileInputStream
        FileInputStream fileInputStream = null;
        R r = null;
        try {
            fileInputStream = (FileInputStream) file.getInputStream();
            r = startupProjectService.readExcel(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭fileInputStream
            CloseStream.closeFileInputStream(fileInputStream);
        }
        return r;
    }

    /**
     * 导出excel
     * clr
     *
     * @return
     */
    @GetMapping("/writeExcel")
    public R writeExcel(HttpServletResponse response, StartupProject startupProject) throws IOException {
        R r = startupProjectService.writeExcel(startupProject,response);
        return r;
    }

    /**
     * 查询所有
     * clr
     *
     * @return
     */
    @GetMapping("/getStartupProject")
    public R selectAllStartupInfo(int current,int size) {
        R r = startupProjectService.selectAllStartupInfo(current,size);
        return r;
    }

    /**
     *  条件查询
     * @return
     */
    @PostMapping("/selectStartupProjectByCondition")
    public R startupConditionalQuery(@RequestBody String teacherCondition,int current,int size){
//        System.out.println("--------------------------" + teacherCondition);
        String s = "{'name':'氨基酸'}";
        R r = startupProjectService.startupConditionalQuery(teacherCondition,current,size);
        return r;
    }

    /**
     * 根据id更新教师信息
     * @return
     */
    @PutMapping("/putStartupProject")
    public R updateStartupInfo(@RequestBody String baseJson){
        R r = startupProjectService.updateStartupInfo(baseJson);
        return r;
    }

    /**
     * 根据id删除教师信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteStartupProject")
    public R deleteStartupById(@RequestBody String id){
        System.out.println("---------------------"+id);
        R r = startupProjectService.deleteStartupById(id);
        return r;
    }

    /**
     * 增加创新创业操作
     */
    @PostMapping("/postStartupProject")
    public R insertStartup(@RequestBody String startupProject){
        R r=startupProjectService.insertStartup(startupProject);
        return r;
    }

}

